$(function () {
  $('#file-template').fileupload({
    dataType: 'json',
    add: function (e, data) {
      $('#progress-bar').css('width', '0%');
      $('#progress').show();
      data.submit();
    },
    progressall: function (e, data) {
      var progress = parseInt(data.loaded / data.total * 100, 10) + '%';
      $('#progress-bar').css('width', progress);
    },
    done: function (e, data) {
      if (data.result.state == "SUCCESS" || data.result.state == "EXISTS") {
        $("#file-template-img").attr("src", data.result.url).show();
        $("#input-default-template").val(data.result.title);
        $("#control-template").removeClass("has-error has-warning").addClass("has-success");
        $("#control-template .help-block").html("图片上传成功！");
      } else {
        $("#file-template-img").attr("src", "").hide();
        $("#control-template").removeClass("has-success has-warning").addClass("has-error");
        $("#control-template .help-block").html(data.result.state);
      }
      $('#progress').fadeOut();
    },
    error: function (e, data) {
      $("#file-template-img").attr("src", "").hide();
      $("#control-template").removeClass("has-success has-warning").addClass("has-error");
      $("#control-template .help-block").html("图片上传失败！");
      $('#progress').fadeOut();
    }
  });
});

